﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Schema.ScrumTableV1;

#endregion

namespace ScrumTable.DL.Data.Generic
{
    /// <summary>
    /// The data connection section is the main data access point to the
    /// internal data structures. You should use an instance of this class
    /// if you want to modify or access the internal cached data services.
    /// </summary>
    public class DataAccessSection : DisposeBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the connection instance, which is used to retreive data
        /// from the different services.
        /// </summary>
        internal DataConnectionBase Connection { get { return Context.ConnectionInternal; } }

        /// <summary>
        /// Gets the data context which contains the data storage, connection
        /// and all other data specific objects.
        /// </summary>
        internal GenericDataContextBase Context { get; private set; }

        /// <summary>
        /// Gets the root project instance.
        /// </summary>
        internal CacheProject CurrentProject { get; private set; }

        /// <summary>
        /// Gets the iteration instance on the second chain level.
        /// </summary>
        internal CacheIteration CurrentIteration { get; private set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataConnectionSection instance, no public constructor
        /// needed.
        /// </summary>
        /// <remarks>
        /// Enters a critical section which must be used to access
        /// an element from the TFS connection context.
        /// This method must be used in conjunction with the using()
        /// statement.
        /// </remarks>
        internal protected DataAccessSection(GenericDataContextBase context, ValueChain<string> parentChain)
        {
            PreCondition.AssertNotNull(context, "context");
            
            Context = context;

            if (parentChain != null)
                InitProject(parentChain);
        }

        /// <summary>
        /// Creates a new DataConnectionSection instance, no public constructor
        /// needed.
        /// </summary>
        /// <remarks>
        /// Enters a critical section which must be used to access
        /// an element from the TFS connection context.
        /// This method must be used in conjunction with the using()
        /// statement.
        /// </remarks>
        internal protected DataAccessSection(GenericDataContextBase context)
            : this(context, null)
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        
        private void InitProject(ValueChain<string> parentChain)
        {
            if (ValueChain<string>.IsNullOrEmpty(parentChain))
                return;

            Pair<CacheProject, CacheIteration> dataPair = Connection.DataCache.GetProjectAndIteration(
                parentChain.Bottom,
                parentChain.TryGetValue(1));

            CurrentProject = dataPair.Left;
            CurrentIteration = dataPair.Right;

            if (CurrentProject == null)
                return;
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}